package SEApp
{
	import SEApp.dialogs.se_alert;
	import SEApp.dialogs.se_dialog;
	import SEApp.dialogs.se_promt;
	
	import UIFree.ObjectEvent;
	import UIFree.components.uif_window;
	import UIFree.controls.uif_button;
	import UIFree.controls.uif_label;
	import UIFree.uif_app;
	import UIFree.uif_ecomponent;
	import UIFree.uif_styles;
	
	import api_servers.api_server;
	import flash.events.Event;
	import flash.events.EventDispatcher;

	public class se_app extends uif_app {
		public static var DIALOG_LAYER: uint = 4;
		
		private var _alert				: se_alert;
		private var _user				: Object;
		protected var _externalServer	: api_server;
		protected var _internalServer	: api_server;
		
		protected var _layers			: Array;
		
		public static const UPDATEUSER: String = 'UPDATEUSER';
		
		public function se_app(appWidth: uint, appHeight: uint, layerCount: uint =6) {
			super(getStyles());
			styleClass = 'application';
			resize(appWidth, appHeight);
			initLayers(layerCount);
			_user = new Object();
			function l_start(e: Event=null): void {
				removeEventListener(Event.ADDED_TO_STAGE, l_start);
				initialize();
			}
			if (!stage) addEventListener(Event.ADDED_TO_STAGE, l_start);
			else l_start();
		}
		
		protected function initialize(): void {
			_externalServer = createExternalServer();
			_internalServer = createInternalServer();
		}
		
		public function get user(): Object {
			return _user;
		}		

		public function set user(a_user: Object): void {
			_user = a_user;
			dispatchEvent(new ObjectEvent(UPDATEUSER, _user));
		}
				
		public function get externalServer(): api_server {
			return _externalServer;
		}
		
		public function get internalServer(): api_server {
			return _internalServer;
		}
		
		protected function createExternalServer(): api_server {
			return null;
		}
		
		protected function createInternalServer(): api_server {
			return null;
		}
		
		override public function get classes(): Object {
			return {
				alert		: se_alert,
				promt		: se_promt,
				button		: uif_button,
				textArea	: uif_label,
				data		: se_data,
				window		: uif_window,
				dialog		: se_dialog
			}
		}
		
		protected function getStyles(): uif_styles {
			return null;
		}
		
		protected function initLayers(layerCount: uint=6): void {
			_layers = new Array();
			for (var i: int =0; i<layerCount; i++) {
				_layers[i] = se_util.createComponent(this, uif_ecomponent);
			}
		}
		
		public function get topLayer(): uif_ecomponent {
			return _layers[_layers.length - 1];
		}
		
		public function get layers(): Array {
			return _layers;
		}
		
		public function get dialogLayer(): uif_ecomponent {
			return layers[DIALOG_LAYER];
		}
		
		public function get popupMenuLayer(): uif_ecomponent {
			return layers[DIALOG_LAYER - 1];
		}
		
		public function get toolsBarLayer(): uif_ecomponent {
			return layers[DIALOG_LAYER - 2];
		}
		
		public function promt(title: String, message: String, editText: String,
							closeFunc: Function =null): se_promt {
			var promt: se_promt = new classes.promt(dialogLayer, title, message, editText, closeFunc);
			promt.show();
			return promt;
		}
		
		public function albumsPrepare(uid: String, albums: Array): Array {
			return se_util.albumsPrepare(albums, uid != externalServer.viewer_id);
		}
		
		public function alert(title: String, message: String, 
							closeFunc: Function =null, cancelButton: Boolean = false,
							alertClass: Class = null): se_alert {
			
			function showNewAlert(): void {
				if (alertClass == null) alertClass = app.classes.alert;
				_alert = new alertClass(dialogLayer, message, closeFunc, cancelButton);
				_alert.title.text = title;
				_alert.show();
				_alert.addEvent(uif_window.CLOSEWINDOW, function(e: Event): void {
					_alert = null;
				});
			}
			
			if (_alert && !_alert.cancelButton.visible) {
				_alert.close(0, function(): void {
					showNewAlert();
				});
			} else showNewAlert();
			return _alert;
		}
	}
}